home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Games / Tetris / Source / RCS / ScoreKeeper.m,v < prev    next >
Encoding:
Text File  |  1972-08-29  |  8.4 KB  |  434 lines

  1. head     1.4;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ;
  6. comment  @@;
  7.  
  8.  
  9. 1.4
  10. date     92.03.01.05.07.41;  author melling;  state Exp;
  11. branches ;
  12. next     1.3;
  13.  
  14. 1.3
  15. date     92.02.28.05.18.58;  author melling;  state Exp;
  16. branches ;
  17. next     1.2;
  18.  
  19. 1.2
  20. date     91.12.16.17.22.09;  author melling;  state Exp;
  21. branches ;
  22. next     1.1;
  23.  
  24. 1.1
  25. date     91.12.07.03.36.59;  author melling;  state Exp;
  26. branches ;
  27. next     ;
  28.  
  29.  
  30. desc
  31. @Tetris 1.1
  32. @
  33.  
  34.  
  35. 1.4
  36. log
  37. @*** empty log message ***
  38. @
  39. text
  40. @
  41. #import <appkit/defaults.h>
  42. #import <appkit/Matrix.h>
  43. #import <appkit/TextField.h>
  44. #import <appkit/TextFieldCell.h>
  45. #import <appkit/Window.h>
  46. #import <pwd.h>
  47. #import <strings.h>
  48. #import <sys/types.h>
  49. #import <ctype.h>
  50. #import "ScoreKeeper.h"
  51. #import "TetApp.h"
  52.  
  53. #define SCORE_FILE    ".TetrisScores"
  54.  
  55. #define NUM_SCORES    10
  56. #define BEZEL_SIZE    2.0
  57. #define FIELD_X_OFFSET    2.0
  58.  
  59. extern void setpwent();
  60. extern void endpwent();
  61. extern uid_t getuid();
  62. extern void chmod(char *path, int mode);
  63.  
  64. @@implementation ScoreKeeper
  65. #ifdef FOO
  66. - (int) fileFoundProc(char *)path
  67. {
  68.     strcpy(self->path, path);
  69.     return 0;
  70. }
  71. #endif
  72.  
  73. - (NXStream *)findScoresFile:(int) mode
  74. {
  75.     NXStream *result;
  76.     char *appName;
  77.     
  78.     strcpy(path, NXArgv[0]);
  79.     if ((appName = rindex(path, '/')))
  80.       appName++;
  81.     else 
  82.       appName = path;
  83.     strcpy(appName, SCORE_FILE);
  84.     if (!(result = NXMapFile(path, mode))) {
  85.         strcat(strcat(strcpy(path, NXHomeDirectory()), "/"), SCORE_FILE);
  86.         result = (mode == NX_READONLY) ? NXMapFile(path, NX_READONLY) :
  87.         NXOpenMemory(NULL, 0, mode);
  88.     }
  89.     return result;
  90. }
  91.  
  92. - init
  93. {
  94.     [super init];
  95.     nameField = nil;
  96.     return self;
  97. }
  98.  
  99. - (void) readHighScores:(NXStream *)stream
  100. {
  101.     int c;
  102.     char name[BUFSIZ];
  103.     char level[5];
  104.     char myScore[10];
  105.     
  106.     for (c = 0; c < NUM_SCORES; c++) {
  107.         name[0] = level[0] = myScore[0] = '\0';
  108.         NXScanf(stream, "%[^\7]\7%[^\7]\7%[^\7]\7", name, level, myScore);
  109.         [[names cellAt:c :0] setStringValue:name],
  110.         [[levels cellAt:c :0] setStringValue:level],
  111.         [[scores cellAt:c :0] setStringValue:myScore];
  112.     }
  113. }
  114.  
  115. - setScoresWindow:anObject
  116. {
  117.     NXStream *inStream;
  118.     
  119.     scoresWindow = anObject;
  120.     [scoresWindow setDelegate:self];
  121.     if ((inStream = [self findScoresFile: NX_READONLY])) {
  122.         [self readHighScores: inStream];
  123.         NXCloseMemory(inStream, NX_FREEBUFFER);
  124.     }
  125.     return self;
  126. }
  127.  
  128. - setZero
  129. {
  130.     [score setIntValue:0];
  131.     return self;
  132. }
  133.  
  134. - addScore:(int)amount
  135. {
  136.     [score setIntValue:[score intValue] + amount];
  137.     return self;
  138. }
  139.  
  140. static char *username()
  141. {
  142.     static char *username = (char *)0;
  143.     struct passwd *pwdentry;
  144.     static char ubuf [128];
  145.     
  146.     if (!username) {
  147.         char * pc;
  148.  
  149.         setpwent();
  150.         pwdentry = getpwuid(getuid());
  151.         endpwent();
  152.         username = ((pwdentry) ? pwdentry->pw_gecos : "");
  153.         /* extended GECOS field handling added by Detlev Droege,
  154.         * droege@@infko.uni-koblenz.de, December 1991 */
  155.         if (pc = index (username, ','))
  156.           *pc = 0;        /* strip ','-part of gecos field */
  157.         if (index (username, '&')) {
  158.             /* do '&' expansion */
  159.             pc = ubuf;
  160.             while (*username) {
  161.                 if (*username == '&') {
  162.                     strcpy (pc, pwdentry->pw_name);
  163.                     *pc = toupper (*pc);
  164.                     while (*pc) pc++;
  165.                 }
  166.                 else
  167.                   *pc++ = *username;
  168.                 username++;
  169.             }
  170.             *pc = 0;
  171.             username = ubuf;
  172.         }
  173.     }
  174.     return username;
  175. }
  176.  
  177. - (int) findrank:(int) myScore
  178. {
  179.     id window = [names window];
  180.     int myRank;
  181.     int c;
  182.  
  183.     [window disableFlushWindow];
  184.     for (myRank = 0; myRank < NUM_SCORES; myRank++)
  185.       if (myScore >= [[scores cellAt:myRank :0] intValue])
  186.          break;
  187.     for (c = NUM_SCORES - 1; c > myRank; c--) {
  188.         [[names cellAt:c :0] setStringValue:
  189.          [[names cellAt:(c - 1) :0] stringValue]];
  190.  
  191.         [[levels cellAt:c :0] setStringValue:
  192.          [[levels cellAt:(c - 1) :0] stringValue]];
  193.  
  194.         [[scores cellAt:c :0] setStringValue:
  195.          [[scores cellAt:(c - 1) :0] stringValue]];
  196.     }
  197.     [window reenableFlushWindow];
  198.     return myRank;
  199. }
  200.  
  201. - updateHighScores:(int)level
  202. {
  203.     id window = [names window];
  204.     int scoreValue = [score intValue];
  205.     NXRect nRect;
  206.     
  207.     if ((rank = [self findrank: scoreValue]) < NUM_SCORES) {
  208.         [scoresWindow makeKeyAndOrderFront:self];
  209.         [[levels cellAt:rank :0] setIntValue:level];
  210.         [[scores cellAt:rank :0] setIntValue:scoreValue];
  211.         [[names getCellFrame:&nRect at:rank :0] convertRect:&nRect toView:nil];
  212.         NXInsetRect(&nRect, -BEZEL_SIZE, -BEZEL_SIZE);
  213.         nRect.origin.x -= FIELD_X_OFFSET;
  214.  
  215.         if (!nameField) {
  216.             nameField = [[TextField alloc] initFrame:&nRect];
  217.             [nameField setEditable:YES];
  218.             [[nameField setTarget:self] setAction:@@selector(nameFieldEnd:)];
  219.         } else
  220.           [nameField moveTo:nRect.origin.x :nRect.origin.y];
  221.  
  222.         [nameField setStringValue:username()];
  223.         [[window contentView] addSubview:nameField];
  224.         [[nameField display] selectText:self];
  225.         while ([NXApp peekAndGetNextEvent:NX_KEYUPMASK | NX_KEYDOWNMASK]) ;
  226.         [NXApp runModalFor:window];
  227.     }
  228.     return self;
  229. }
  230.  
  231. - nameFieldEnd:sender
  232. {
  233.     id window = [names window];
  234.     
  235.     [window disableFlushWindow];
  236.     [[names cellAt:rank :0] setStringValue:[nameField stringValue]];
  237.     [nameField removeFromSuperview];
  238.     [window display];
  239.     [[window reenableFlushWindow] flushWindow];
  240.     [NXApp stopModal];
  241.     return self;
  242. }
  243.  
  244. - windowWillClose:sender
  245. {
  246.     if (nameField && [nameField currentEditor]) {
  247.         [self nameFieldEnd:sender];
  248.         return nil;
  249.     } else
  250.       return self;
  251. }
  252.  
  253. - writeHighScores:(NXStream *)stream
  254. {
  255.     int c;
  256.     
  257.     for (c = 0; c < NUM_SCORES; c++)
  258.         NXPrintf(stream, "%s\7%s\7%s\7",
  259.                     [[names cellAt:c :0] stringValue],
  260.                     [[levels cellAt:c :0] stringValue],
  261.                     [[scores cellAt:c :0] stringValue]);
  262.     NXFlush(stream);
  263.      return self;
  264. }
  265.  
  266. - writeScores
  267. {
  268.     NXStream *outStream;
  269.  
  270.     if ((outStream = [self findScoresFile: NX_WRITEONLY])) {
  271.         [self writeHighScores: outStream];
  272.         NXSaveToFile(outStream, path);
  273.         NXCloseMemory(outStream, NX_FREEBUFFER);
  274.         chmod(path, 0666);
  275.     }
  276.     return self;
  277. }
  278.  
  279. - free
  280. {
  281.     if (nameField)
  282.         [nameField free];
  283.     return [super free];
  284. }
  285.  
  286. @@end
  287.  
  288. @
  289.  
  290.  
  291. 1.3
  292. log
  293. @*** empty log message ***
  294. @
  295. text
  296. @d10 1
  297. d78 1
  298. a78 1
  299.     NXStream *inStream;
  300. d80 7
  301. a86 7
  302.     scoresWindow = anObject;
  303.     [scoresWindow setDelegate:self];
  304.     if ((inStream = [self findScoresFile: NX_READONLY])) {
  305.          [self readHighScores: inStream];
  306.          NXCloseMemory(inStream, NX_FREEBUFFER);
  307.     }
  308.     return self;
  309. d91 2
  310. a92 2
  311.     [score setIntValue:0];
  312.     return self;
  313. d105 1
  314. d108 2
  315. d114 20
  316. @
  317.  
  318.  
  319. 1.2
  320. log
  321. @*** empty log message ***
  322. @
  323. text
  324. @d64 1
  325. a64 1
  326.     char score[10];
  327. d67 2
  328. a68 2
  329.         name[0] = level[0] = score[0] = '\0';
  330.         NXScanf(stream, "%[^\7]\7%[^\7]\7%[^\7]\7", name, level, score);
  331. d71 1
  332. a71 1
  333.         [[scores cellAt:c :0] setStringValue:score];
  334. d114 1
  335. a114 1
  336. - (int) findrank:(int) score
  337. d117 1
  338. a117 1
  339.     int rank;
  340. d119 1
  341. a119 1
  342.     
  343. d121 2
  344. a122 2
  345.     for (rank = 0; rank < NUM_SCORES; rank++)
  346.       if (score >= [[scores cellAt:rank :0] intValue])
  347. d124 1
  348. a124 1
  349.     for (c = NUM_SCORES - 1; c > rank; c--) {
  350. d135 1
  351. a135 1
  352.     return rank;
  353. @
  354.  
  355.  
  356. 1.1
  357. log
  358. @Initial revision
  359. @
  360. text
  361. @d25 2
  362. a26 2
  363.  
  364. static int fileFoundProc(char *path, id self)
  365. d31 1
  366. d33 1
  367. a33 1
  368. static NXStream *findScoresFile(id self, int mode)
  369. d35 12
  370. a46 12
  371.     NXStream *result;
  372.     char *appName;
  373.     
  374.     strcpy(self->path, NXArgv[0]);
  375.     if ((appName = rindex(self->path, '/')))
  376.         appName++;
  377.     else 
  378.         appName = self->path;
  379.     strcpy(appName, SCORE_FILE);
  380.     if (!(result = NXMapFile(self->path, mode))) {
  381.     strcat(strcat(strcpy(self->path, NXHomeDirectory()), "/"), SCORE_FILE);
  382.     result = (mode == NX_READONLY) ? NXMapFile(self->path, NX_READONLY) :
  383. d48 2
  384. a49 2
  385.     }
  386.     return result;
  387. d59 1
  388. a59 1
  389. static void readHighScores(id self, NXStream *stream)
  390. d69 3
  391. a71 3
  392.         [[self->names cellAt:c :0] setStringValue:name],
  393.         [[self->levels cellAt:c :0] setStringValue:level],
  394.         [[self->scores cellAt:c :0] setStringValue:score];
  395. d81 2
  396. a82 2
  397.     if ((inStream = findScoresFile(self, NX_READONLY))) {
  398.          readHighScores(self, inStream);
  399. d114 1
  400. a114 1
  401. static int findrank(id self, int score)
  402. d116 1
  403. a116 1
  404.     id window = [self->names window];
  405. d122 1
  406. a122 1
  407.       if (score >= [[self->scores cellAt:rank :0] intValue])
  408. d125 8
  409. a132 6
  410.         [[self->names cellAt:c :0] setStringValue:
  411.          [[self->names cellAt:(c - 1) :0] stringValue]];
  412.         [[self->levels cellAt:c :0] setStringValue:
  413.          [[self->levels cellAt:(c - 1) :0] stringValue]];
  414.         [[self->scores cellAt:c :0] setStringValue:
  415.          [[self->scores cellAt:(c - 1) :0] stringValue]];
  416. d144 1
  417. a144 1
  418.     if ((rank = findrank(self, scoreValue)) < NUM_SCORES) {
  419. d190 1
  420. a190 1
  421. writeHighScores(id self, NXStream *stream)
  422. d196 3
  423. a198 3
  424.         [[self->names cellAt:c :0] stringValue],
  425.         [[self->levels cellAt:c :0] stringValue],
  426.         [[self->scores cellAt:c :0] stringValue]);
  427. d200 1
  428. d206 3
  429. a208 3
  430.     
  431.     if ((outStream = findScoresFile(self, NX_WRITEONLY))) {
  432.         writeHighScores(self, outStream);
  433. @
  434.